package org.jet.emall.rbac.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jet.emall.common.constant.PrivilegeType;
import org.jet.emall.rbac.dto.AdminOperationDTO;
import org.jet.emall.rbac.po.*;
import org.jet.emall.rbac.service.mp.*;
import org.jet.emall.rbac.transfer.AdminOperationDtoTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-09 14:47
 */
@Service
public class CustomAdminOperationServiceImpl implements CustomAdminOperationService {

    @Autowired
    private IAdminOperationService adminOperationService;

    @Autowired
    private IAdminPrivilegeService adminPrivilegeService;

    @Autowired
    private IAdminPrivilegeOperationService adminPrivilegeOperationService;

    @Autowired
    private IAdminPrivilegeMenuService adminPrivilegeMenuService;

    @Autowired
    private IAdminRoleService adminRoleService;

    @Autowired
    private IAdminRolePrivilegeService adminRolePrivilegeService;

    @Override
    public long count() {
        return adminOperationService.count();
    }

    @Override
    public void save(List<AdminOperationDTO> list) {
        adminOperationService.saveBatch(AdminOperationDtoTransfer.INSTANCE.toPoList(list));
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void assignAllPrivilegesToEveryRole(){
        List<AdminOperation> list = adminOperationService.list();
        list.forEach(adminOperation -> {
            //将所有具有归属菜单的操作全部加入权限表tb_admin_privilege
            if(adminOperation.getMenuId()!=null){
                AdminPrivilege adminPrivilege = new AdminPrivilege();
                adminPrivilege.setTyp(PrivilegeType.OPERATION.getCode());
                Wrapper<AdminPrivilegeMenu> wrapper = new QueryWrapper<>();
                ((QueryWrapper<AdminPrivilegeMenu>) wrapper).eq("mid",adminOperation.getMenuId());
                AdminPrivilegeMenu pm = adminPrivilegeMenuService.getOne(wrapper);
                adminPrivilege.setSuperId(pm.getPid());
                adminPrivilegeService.save(adminPrivilege);
                AdminPrivilegeOperation privilegeOperation = new AdminPrivilegeOperation();
                privilegeOperation.setOid(adminOperation.getId());
                privilegeOperation.setPid(adminPrivilege.getId());
                adminPrivilegeOperationService.save(privilegeOperation);
            }
        });
        List<AdminPrivilege> privilegeList = adminPrivilegeService.list();
        List<AdminRole> roleList = adminRoleService.list();
        privilegeList.forEach(adminPrivilege -> {
            roleList.forEach(adminRole -> {
                AdminRolePrivilege adminRolePrivilege = new AdminRolePrivilege();
                adminRolePrivilege.setPid(adminPrivilege.getId());
                adminRolePrivilege.setRid(adminRole.getId());
                adminRolePrivilegeService.save(adminRolePrivilege);
            });
        });
    }
}
